<?php

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

// name configurable
class plugin_dmarkd {

// -----------
// functionals	
// -----------
	function _regex_url_make($type) { return "#http(s?)://(.*)\.".type."#"; }  // construct a regex
	function _script_string_make($script_url) { return '<script src="'.$script_url.'"></script>'; }	// construct a <script> string with src mark
	function _stylesheet_string_make($stylesheet_url) { return '<link rel="stylesheet" href="'.$stylesheet_url.'"></link>'; } 	// construct a <link> string with rel mark set as "stylesheet" and href mark



// -------------------------
// real effect-ful functions
// -------------------------
	// pre-process the key parameters
	function common() {
		global $_G;
		$this->plugin_cache = $_G['cache']['plugin']['dmarkd'];  // set alias for cache
		$this->pluginurl = $_G['siteurl']."source/plugin/dmarkd/";	// get this plugin's location, configurable

		// inline default settings
		$this->showdown = array(
			'source' => "https://cdnjs.cloudflare.com/ajax/libs/showdown/1.9.1/showdown.min.js",
		);

		// configure user settings
		$type = ['js', 'source'];
		if (empty($plugin_cache[type[1]])) {
			if (preg_match($this->_regex_url_make(type[0]), $plugin_cache[type[1]])) {
				// match successful, make user settings valid
				$this->showdown[type[1]] = $plugin_cache[type[1]];
			}//if preg
		}//if empty
	}//function



	// load showdown script on critical pages
	function global_footer() {
		$script = '';
		
		// critical pages check
		if (CURMODULE == 'viewthread' || CURMODULE == 'forumdisplay' || CURMODULE == 'post' ) {
			// stylesheet and js
			// the url has been set in $this->common()
			foreach(['source'] as $type) {
				$url = $this->showdown[$type];
				if ($type=='stylesheet') $script .= $this->_stylesheet_string_make($url);
				else $script .= $this->_script_string_make($url);
			}//foreach
		}//if CURMODE

		// view thread: renderer
		if (CURMODULE=='viewthread') {
			$script .= $this->_script_string_make($this->pluginurl . "js/autoRender.js");
		}
		else if (CURMODULE=='post') {
		// editor button adjusting
			$script .= "
			<style type=\"text/css\">
				.edt .b1r #dmarkdown_editor {
					background:url(".$this->pluginurl."image/markdown.png) no-repeat;background-position:center 1px;
				}
                .edt .b2r #dmarkdown_editor {
                    background:url(".$this->pluginurl."image/markdown_low.png) no-repeat;background-position:center 1px;
                }
			</style>
			";
			$script .= $this->_script_string_make($this->pluginurl . "js/insertion.js");
		}	
		return $script;
	}
}





class plugin_dmarkd_forum extends plugin_dmarkd {

	// add latex button on the editor's control panel
	function post_editorctrl_left() {
		global $_G;
		$button_str = '<a';

		$button_configurations = [
			["id", "dmarkdown_editor"],
			["menupos", "00"],
			["menuwidth", "600"],
			["title", ""],
			["menupos", "00"],
			["menuwidth", "600"],
			["onclick", "showWindow(\"markdown_edit_window\", \"plugin.php?id=dmarkd:dmarkd_editor&adds=e_iframe\");"],
		];

		foreach ($button_configurations as $properties) {
			$button_str .= " ".$properties[0]."="."'".$properties[1]."'";
		}

		$button_str .= ">MD</a>";

		// name configurable
		return $button_str;
	}
}

?>

